namespace csk.ntv.grib
{
    public class WindHeader
    {
        /// <summary> 参数类别 </summary>
        public int parameterCategory { get; set; }
        /// <summary> 参数编码 </summary>
        public int parameterNumber { get; set; }
        /// <summary> 参数名称 </summary>
        public string parameterNumberName { get; set; }
        /// <summary> 参数单位 </summary>
        public string parameterUnit { get; set; }
        /// <summary> x方向格点树 </summary>
        public int nx { get; set; }
        /// <summary> y方向格点数 </summary>
        public int ny { get; set; }
        /// <summary> x方向间隔 </summary>
        public float dx { get; set; }
        /// <summary> y方向间隔 </summary>
        public float dy { get; set; }
        /// <summary> 经度范围 (W->E) </summary>
        public float lo1 { get; set; }
        public float lo2 { get; set; }
        /// <summary> 纬度范围 (N->S) </summary>
        public float la1 { get; set; }
        public float la2 { get; set; }

        /// <summary> 参考时间 e.g. "2017-02-01 23:00:00</summary>
        public string refTime { get; set; }

        // {{END}}
    }

    // 风模型
    public class WindModel
    {
        public WindHeader header { get; set; }
        public double[] data { get; set; }
    }




}

// export interface IWindyHeader {
//     /** 基础角度 */
//     basicAngle?: number,    // 0,
//     /** 中心代码 */
//     center?: number,
//     /** 中心名称 */
//     centerName?: string,    //"US National Weather Service - NCEP(WMC)",
//     /** 学科代码 */
//     discipline?: number,
//     /** 学科名称 */
//     disciplineName?: number,// "Meteorological products",
//     /** 预报时间 */
//     forecastTime?: number,
//     /** ? */
//     genProcessType?: number,
//     /** ? */
//     genProcessTypeName?: string //"Forecast",
//     /** GRIB 版本 */
//     gribEdition?: number,
//     /** GRIB 长度 */
//     gribLength?: number,
//     /** 格点定义模板 */
//     gridDefinitionTemplate?: number,
//     /** 格点定义模板名称 */
//     gridDefinitionTemplateName?: string, //"Latitude_Longitude",
//     /** 格点单位 */
//     gridUnits?: "degrees",
//     /** 产品状态 */
//     productStatus?: number,
//     /** 产品状态名称 */
//     productStatusName?: string, //"Operational products",
//     /** 产品类型 */
//     productType: number,
//     /** 产品类型名称 */
//     productTypeName: string,    //"Forecast products",
//     /** 产品定义模板 */
//     productDefinitionTemplate?: number,
//     /** 产品定义模板名称 */
//     productDefinitionTemplateName?: string, //"Analysis/forecast at horizontal level/layer at a point in time",
//     /** RT 意义 */
//     significanceOfRT?: number,
//     /** RT 名称 */
//     significanceOfRTName?: string,   //"Start of forecast",

//     // ------------------------------------------
//     /** 参数类别 */
//     parameterCategory: number,
//     /** 参数类别名称 */
//     parameterCategoryName?: string,
//     /** 参数代码 */
//     parameterNumber: number,
//     /** 参数名称 */
//     parameterNumberName: string,
//     /** 参数单位 */
//     parameterUnit: string,
//     /** x方向格点树 */
//     nx: number,
//     /** y方向格点数 */
//     ny: number,
//     /** x方向间隔 */
//     dx: number,
//     /** y方向间隔 */
//     dy: number,
//     /** 经度范围 (W->E) */
//     lo1: number,
//     lo2: number,
//     /** 纬度范围 (N->S) */
//     la1: number,
//     la2: number,

//     /** 参考时间 */
//     refTime: string,    // e.g. "2017-02-01 23:00:00",
//     // -------------------------------------------------

//     /** 点数量 */
//     numberPoints?: number,
//     /** ？ */
//     resolution?: number,    // 48 ?
//     /** 扫描模式 */
//     scanMode?: number,
//     /** 形状 */
//     shape?: number,
//     /** 形状名称 */
//     shapeName?: string,     //"Earth spherical with radius of 6,371,229.0 m",

//     /** 子中心编码 */
//     subcenter: number,
//     /** 子部门编码 */
//     subDivisions?: number,

//     /** 地表1类型 */
//     surface1Type?: number,
//     /** 地表1类型名称 */
//     surface1TypeName?: string, //"Specified height level above ground",
//     /** 地表1值 */
//     surface1Value?: number,
//     /** 地表2类型 */
//     surface2Type?: number,
//     /** 地表2类型名称 */
//     surface2TypeName?: string, //"Missing",
//     /** 地表2值 */
//     surface2Value?: number,

//     /** 是否风场 */
//     winds?: boolean,

//     // {{END}}
// }

// export interface IWindyModel {
//     header: IWindyHeader,
//     data: number[],
// }



/*
# -*- coding: UTF-8 -*-
import sys
import eccodes
import numpy as np
from datetime import datetime, timedelta

def getVar(file, varname):
    ### file表示要读取的GRIB文件(字符串类型)
    ### varname表示要读取的变量名(字符串类型)     
    var_summary = dict() #存储变量信息   

    iid = eccodes.codes_index_new_from_file(file, keys=['shortName'])
    vars_all = eccodes.codes_index_get(iid, key='shortName') #all variables in the file
    if varname not in vars_all:
        print("Error: {0} is not in the list of variables")
        sys.exit()
    var_summary['shortname'] = varname

    eccodes.codes_index_select(iid, key='shortName', value=varname)
    gid = eccodes.codes_new_from_index(iid)

    ### variable type
    try:
        level_type = 'sfc' if varname=='ptype' else eccodes.codes_get(gid, 'indicatorOfTypeOfLevel')
        var_summary['type'] = 'surface' if level_type=='sfc' else 'pressure levels(hPa)'
    except:
        print('The function only read the surface or pressure level variables, {0} is model level variable' 
              .format(varname))
        sys.exit()

    ### time info
    ymdh_start_utc = str(eccodes.codes_get(gid, 'dataDate')) + '%02d'%(eccodes.codes_get(gid, 'dataTime')//100)
    delta_hours = int(eccodes.codes_get(gid, 'endStep'))

    ymdh_start_bj_date = datetime.strptime(ymdh_start_utc,'%Y%m%d%H') + timedelta(hours=8) #transform from utc to beijing
    ymdh_pred_bj_date = ymdh_start_bj_date + timedelta(hours=delta_hours)

    ymdh_start_bj = ymdh_start_bj_date.strftime('%Y%m%d%H')
    ymdh_pred_bj = ymdh_pred_bj_date.strftime('%Y%m%d%H')
    var_summary['start_time'] = ymdh_start_bj
    var_summary['predict_time'] = ymdh_pred_bj

    ### long name and unit
    long_name = eccodes.codes_get(gid, 'name')
    unit = eccodes.codes_get(gid, 'units')
    var_summary['longname'] = long_name
    var_summary['unit'] = unit

    ### lat/lon info
    lat_ec_start = eccodes.codes_get(gid, 'latitudeOfFirstGridPointInDegrees')
    lat_ec_end = eccodes.codes_get(gid, 'latitudeOfLastGridPointInDegrees')
    lat_ec_delta = eccodes.codes_get(gid, 'jDirectionIncrementInDegrees')
    s_to_n = False if eccodes.codes_get(gid, 'jScansPositively')==0 else True

    lon_ec_start = eccodes.codes_get(gid, 'longitudeOfFirstGridPointInDegrees')
    lon_ec_end = eccodes.codes_get(gid, 'longitudeOfLastGridPointInDegrees')
    lon_ec_delta = eccodes.codes_get(gid, 'iDirectionIncrementInDegrees')
    w_to_e = False if eccodes.codes_get(gid, 'iScansNegatively')==1 else True
    lon_ec_start = 360+lon_ec_start if lon_ec_start<0 else lon_ec_start #change to the range 0-360
    lon_ec_end = 360+lon_ec_end if lon_ec_end<0 else lon_ec_end #change to the range 0-360

    var_summary['lat_start'] = lat_ec_start  
    var_summary['lat_end'] = lat_ec_end
    var_summary['lat_delta'] = lat_ec_delta
    var_summary['south_to_north'] = s_to_n
    var_summary['lon_start'] = lon_ec_start  
    var_summary['lon_end'] = lon_ec_end
    var_summary['lon_delta'] = lon_ec_delta
    var_summary['west_to_east'] = w_to_e

    ### levels and data values
    levels = []
    data_levels = []
    numlat = eccodes.codes_get(gid, 'Nj')
    numlon = eccodes.codes_get(gid, 'Ni')
    while gid:
        level = eccodes.codes_get(gid, 'level')
        data_ec = eccodes.codes_get_values(gid).reshape(numlat, numlon)
        levels.append(level)
        data_levels.append(data_ec)
        eccodes.codes_release(gid)
        gid = eccodes.codes_new_from_index(iid)
    eccodes.codes_index_release(iid)

    if level_type=='pl': var_summary['levels']=sorted(levels)
    var_summary['values'] = np.array(data_levels)[np.argsort(levels)]

    return var_summary
*/
